package api

import (
	"api-gateway/basic/inits"
	__ "api-gateway/basic/user_proto"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"context"
	"github.com/gin-gonic/gin"
)

// MemberBenefitAdd
// @Summary      会员权益添加
// @Description  会员权益添加
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.MemberBenefitAdd  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /member/memberBenefitAdd [post]
func MemberBenefitAdd(c *gin.Context) {
	var req request.MemberBenefitAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.UserClient.MemberBenefitAdd(context.Background(), &__.MemberBenefitAddReq{
		Name:  req.Name,
		Desc:  req.Desc,
		Types: req.Types,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员权益添加成功", add)

}

// MemberBenefitFind
// @Summary      会员权益展示
// @Description  会员权益展示
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  query      request.MemberBenefitFind  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /member/memberBenefitFind [get]
func MemberBenefitFind(c *gin.Context) {
	var req request.MemberBenefitFind
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	find, err := inits.UserClient.MemberBenefitFind(context.Background(), &__.MemberBenefitFindReq{})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员权益展示成功", find)

}

// MemberBenefitDetail
// @Summary      会员权益详情
// @Description  会员权益详情
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  query      request.MemberBenefitDetail  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /member/memberBenefitDetail [get]
func MemberBenefitDetail(c *gin.Context) {
	var req request.MemberBenefitDetail
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	detail, err := inits.UserClient.MemberBenefitDetail(context.Background(), &__.MemberBenefitDetailReq{Id: req.Id})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员权益详情展示成功", detail)

}

// MemberLevelFind
// @Summary      会员等级展示
// @Description  会员等级展示
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  query      request.MemberLevelFind  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /member/memberLevelFind [get]
func MemberLevelFind(c *gin.Context) {
	var req request.MemberLevelFind
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	find, err := inits.UserClient.MemberLevelFind(context.Background(), &__.MemberLevelFindReq{
		Page:     req.Page,
		PageSize: req.PageSize,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员等级展示成功", find)
}

// MemberLevelDetail
// @Summary      会员等级详情
// @Description  会员等级详情
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  query      request.MemberLevelDetail  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /member/memberLevelDetail [get]
func MemberLevelDetail(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.MemberLevelDetail
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	detail, err := inits.UserClient.MemberLevelDetail(context.Background(), &__.MemberLevelDetailReq{
		UserId: uint64(userId),
		Id:     req.Id})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员等级详情展示成功", detail)
}

// MemberAdd
// @Summary      会员添加
// @Description  会员添加
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.MemberAdd  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /member/memberAdd [post]
func MemberAdd(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.MemberAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	add, err := inits.UserClient.MemberAdd(context.Background(), &__.MemberAddReq{
		UserId: uint64(userId),
		LeveId: req.LeveId,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员添加成功", add)

}

// MemberDetail
// @Summary      用户会员详情
// @Description  用户会员详情
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  query      request.MemberDetail  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /member/memberDetail [get]
func MemberDetail(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.MemberDetail
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	detail, err := inits.UserClient.MemberDetails(context.Background(), &__.MemberDetailsReq{
		UserId: uint64(userId),
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员详情展示成功", detail)
}

// MemberServiceAdd
// @Summary      用户续费
// @Description  用户续费
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.MemberServiceAdd  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /member/memberServiceAdd [post]
func MemberServiceAdd(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.MemberServiceAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.UserClient.MemberServiceAdd(context.Background(), &__.MemberServiceAddReq{
		UserId:   uint64(userId),
		LevelId:  req.LevelId,         // 会员等级ID
		Duration: int32(req.Duration), // 续费时长（月）
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员续费成功", add)
}

// MemberLevelAdd
// @Summary      会员等级添加
// @Description  会员等级添加
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.MemberLevelAdd  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /member/memberLevelAdd [post]
func MemberLevelAdd(c *gin.Context) {
	var req request.MemberLevelAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.UserClient.MemberLevelAdd(context.Background(), &__.MemberLevelAddReq{
		Name:      req.Name,
		Desc:      req.Desc,
		Price:     float32(req.Price),
		Level:     req.Level,
		BenefitId: req.BenefitId,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员等级添加成功", add)
}

// MemberLevelDel
// @Summary      会员等级删除
// @Description  会员等级删除
// @Tags         用户会员模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.MemberLevelDel  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /member/memberLevelDel [post]
func MemberLevelDel(c *gin.Context) {
	var req request.MemberLevelDel
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	del, err := inits.UserClient.MemberLevelDel(context.Background(), &__.MemberLevelDelReq{Id: req.Id})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "会员等级删除成功", del)
}
